namespace SampleApp.Tests {
    using Microsoft.Services.TestTools.UITesting.Html;
    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using SampleApp.Tests.Pages;

    [CodedUITest]
    public class ShopTests : PageTest<Shop> {

        private string _productName;
        private string _category;

        [TestInitialize]
        public override void Initialize() {
            base.Initialize();
            EnsureAtleastOneProduct();
        }

        private void EnsureAtleastOneProduct() {
            var products = Page.Launch<Products>();
            if(!products.ProductsExists()) {
                products.EnsureAtLeastOneCategory();
                products
                    .NavigateToCreateProduct()
                    .CreateNewProduct(Name(), Amount());
                TestedPage.Refresh();
            }
            _productName = products.ProductNameOnRow(1);
            _category    = products.CategoryOnRow(1);
            products.Close();
        }

        [TestMethod]
        public void MessageIsSetWhenCartIsSuccessfullySaved() {
            
            var page = TestedPage
                //first row is the header row
                .AddCartItem(rowInstance: 2, categoryName: _category, productName: _productName, quantity: 5)
                .AddProduct()
                .AddCartItem(rowInstance: 3, categoryName: _category, productName: _productName, quantity: 10)
                .SubmitOrder();

            Assert.IsTrue(page.MessageIsNotEmpty(), "The message was empty");

        }

    }
}